package com.xk.controller;

import com.xk.pojo.Student;

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class studentController {
    ArrayList<Student> students = new ArrayList<>();
    Scanner sc = new Scanner(System.in);

    // 显示菜单
    public void menu() {
        while (true) {
            System.out.println("\n************************************\n" +
                    "*           学生信息管理系统          *\n" +
                    "************************************\n" +
                    "* [1] 添加学生信息\t" +
                    "[2] 删除学生信息 *\n" +
                    "* [3] 修改学生信息\t" +
                    "[4] 查找学生信息 *\n" +
                    "* [5] 显示学生信息\t" +
                    "[6] 保存学生信息 *\n" +
                    "* [7] 导入学生信息\t" +
                    "[0] 退出系统    *\n" +
                    "************************************");
            System.out.println("请输入你的选择(数字)：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    foundStudent();
                    break;
                case 5:
                    disPlayStudent();
                    break;
                case 6:
                    saveStudent();
                    break;
                case 7:
                    importStudent();
                    break;
                case 0:
                    System.out.println("提示：已退出系统！");
                    return;
                default:
                    System.out.println("提示：输入错误，请重新输入!\n");
            }
        }
    }

    // 添加学生信息
    public void addStudent() {
        System.out.println("请输入要添加的学生姓名：");
        String addName = sc.next();
        System.out.println("请输入要添加的学生年龄：");
        int addAge = sc.nextInt();
        System.out.println("请输入要添加的学生学号：");
        String addId = sc.next();
        System.out.println("请输入要添加的学生班级：");
        int addClazz = sc.nextInt();
        System.out.println("请输入要添加的学生性别：");
        String addGender = sc.next();

        Student student = new Student(addName, addAge, addId, addClazz, addGender);
        students.add(student);
        System.out.println("提示：学生信息添加成功！");
    }

    // 删除学生信息
    public void deleteStudent() {
        if (students.isEmpty()) {
            System.out.println("提示：学生数据库为空，无法删除！");
            menu();
        }
        System.out.println("请输入你要删除的学生学号：");
        String deleteid = sc.next();
        boolean found = false;
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(deleteid)) {
                students.remove(i);
                found = true;
                System.out.println("提示：该学生已成功删除！");
                break;
            }
        }
        if (!found) {
            System.out.println("提示：没有找到该学生信息~！");
        }
    }

    // 修改学生信息
    public void updateStudent() {
        if (students.isEmpty()) {
            System.out.println("提示：学生数据库为空，无法修改！");
            menu();
        }
        System.out.println("请输入你要修改的学生学号：");
        String upid = sc.next();
        boolean upfound = false;
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(upid)) {
                upfound = true;
                while (true) {
                    System.out.println("请输入你要修改的内容：\n" +
                            "[1] 学生姓名\n" +
                            "[2] 学生年龄\n" +
                            "[3] 学生学号\n" +
                            "[4] 学生班级\n" +
                            "[5] 学生性别\n" +
                            "[0] 返回上一级\n" +
                            "请选择：");
                    int upchoice = sc.nextInt();
                    switch (upchoice) {
                        case 1:
                            System.out.println("请输入你要修改的学生姓名：");
                            String upName = sc.next();
                            students.get(i).setName(upName);
                            System.out.println("提示：学生姓名修改成功!");
                            break;
                        case 2:
                            System.out.println("请输入你要修改的学生年龄：");
                            int upAge = sc.nextInt();
                            students.get(i).setAge(upAge);
                            System.out.println("提示：学生年龄修改成功!");
                            break;
                        case 3:
                            System.out.println("请输入你要修改的学生学号：");
                            String upId = sc.next();
                            students.get(i).setId(upId);
                            System.out.println("提示：学生学号修改成功!");
                            break;
                        case 4:
                            System.out.println("请输入你要修改的学生班级：");
                            int upClazz = sc.nextInt();
                            students.get(i).setClazz(upClazz);
                            System.out.println("提示：学生班级修改成功!");
                            break;
                        case 5:
                            System.out.println("请输入你要修改的学生性别：");
                            String upGender = sc.next();
                            students.get(i).setGender(upGender);
                            System.out.println("提示：学生性别修改成功!");
                            break;
                        case 0:
                            System.out.println("已返回上一级菜单！");
                            return;
                        default:
                            System.out.println("提示：输入错误，请重新输入！");
                    }
                }
            }
        }
        if (!upfound) {
            System.out.println("提示：该学生学号未找到,请重新输入：");
            updateStudent();
        }
    }

    // 查找学生信息
    public void foundStudent() {
        if (students.isEmpty()) {
            System.out.println("提示：学生数据库为空，无法查找！");
            menu();
        }
        System.out.println("请输入你要查找的学生学号：");
        String foundID = sc.next();
        boolean found = false;
        for (Student s : students) {
            if (s.getId().equals(foundID)) {
                found = true;
                System.out.println("提示：已查找到该学生信息！");
                System.out.println(s);
                break;
            }
        }
        if (!found) {
            System.out.println("提示：没有找到该学生信息！");
            foundStudent();
        }
    }

    // 显示学生信息
    public void disPlayStudent() {
        if (students.isEmpty()) {
            System.out.println("提示：学生数据库为空，无法显示！");
            menu();
        }
        System.out.println("学生信息显示如下：");
        for (Student s : students) {
            System.out.println(s);
        }
    }

    // 保存学生信息
    public void saveStudent() {
        if (students.isEmpty()) {
            System.out.println("提示：学生数据库为空，无法保存！");
            menu();
        }

        try {
            BufferedWriter budder = new BufferedWriter(new FileWriter("d://student1.txt"));
            for (Student student : students) {
                // 自定义输出格式，确保与readStudentFile方法中的解析格式一致
                budder.write(student.getName() + "," + student.getAge() + "," + student.getId() + "," + student.getClazz() + "," + student.getGender());
                budder.newLine(); // 换行
            }
            System.out.println("提示：学生信息保存成功！");
            budder.close();
        } catch (IOException e) {
            System.out.println("提示：学生信息保存失败！");
        }
    }

    // 导入文件
    public void importStudent() {
        // 创建 JFileChooser 实例
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("选择学生信息文件");

        // 打开文件选择框
        int userSelection = fileChooser.showOpenDialog(null);

        // 如果用户点击了“打开”
        if (userSelection == JFileChooser.APPROVE_OPTION) {
            File fileToOpen = fileChooser.getSelectedFile();
            if (fileToOpen.exists() && fileToOpen.canRead()) {
                readStudentFile(fileToOpen);
            } else {
                System.out.println("文件不存在或无法读取。");
            }
        } else {
            System.out.println("未选择任何文件。");
        }
    }

    private void readStudentFile(File file) {
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            int lineCount = 0;
            while ((line = br.readLine()) != null) {
                lineCount++;
                String[] parts = line.split(","); // 假设使用逗号分隔
                if (parts.length == 5) { // 假设每一行包含5个字段
                    String name = parts[0].trim();
                    int age = Integer.parseInt(parts[1].trim());
                    String id = parts[2].trim();
                    int clazz = Integer.parseInt(parts[3].trim());
                    String gender = parts[4].trim();
                    Student student = new Student(name, age, id, clazz, gender);
                    students.add(student);
                } else {
                    System.out.println("导入数据格式错误 (行 " + lineCount + "): " + line);
                }
            }
            System.out.println("学生信息导入成功！共导入 " + students.size() + " 位学生。");
        } catch (IOException e) {
            System.out.println("文件读取错误：" + e.getMessage());
        } catch (NumberFormatException e) {
            System.out.println("年龄或班级格式错误，请确保年龄和班级是整数。错误信息: " + e.getMessage());
        }
    }
}
